package com.itheima.dao.impl;

import com.itheima.bean.User;
import com.itheima.dao.UserMapper;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserImpl implements UserMapper {

    @Override//用户登录用  检查用户名和密码是否正确
    public User selectUser(String name, String passWord) {
        //获得普通的会话
        SqlSession session = MyBatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectUser(name, passWord);
        //关闭会话
        session.close();
        return user;
    }

    @Override//通过用户名查询用户是否存在
    public User selectUserByName(String name) {
        //获得普通的会话
        SqlSession session = MyBatisUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.selectUserByName(name);
        //关闭会话
        session.close();
        return user;
    }

    @Override
    public void addUser(User user) {
        //获得自动提交事务的会话
        SqlSession session = MyBatisUtils.getAutoSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.addUser(user);
        //关闭会话
        session.close();
    }
}
